/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.json.ref"]){
dojo._hasResource["dojox.json.ref"]=true;
dojo.provide("dojox.json.ref");
dojo.require("dojo.date.stamp");
dojox.json.ref={resolveJson:function(_1,_2){
_2=_2||{};
var _3=_2.idAttribute||"id";
var _4=this.refAttribute;
var _5=_2.idPrefix||"";
var _6=_2.assignAbsoluteIds;
var _7=_2.index||{};
var _8=_2.timeStamps;
var _9,_a=[];
var _b=/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/;
var _c=this._addProp;
var F=function(){
};
function _d(it,_e,_f,_10,_11,_12){
var i,_13,val,id=_3 in it?it[_3]:_f;
if(_3 in it||((id!==undefined)&&_10)){
id=(_5+id).replace(_b,"$2$3");
}
var _14=_12||it;
if(id!==undefined){
if(_6){
it.__id=id;
}
if(_2.schemas&&(!(it instanceof Array))&&(val=id.match(/^(.+\/)[^\.\[]*$/))){
_11=_2.schemas[val[1]];
}
if(_7[id]&&((it instanceof Array)==(_7[id] instanceof Array))){
_14=_7[id];
delete _14.$ref;
delete _14._loadObject;
_13=true;
}else{
var _15=_11&&_11.prototype;
if(_15){
F.prototype=_15;
_14=new F();
}
}
_7[id]=_14;
if(_8){
_8[id]=_2.time;
}
}
while(_11){
var _16=_11.properties;
if(_16){
for(i in it){
var _17=_16[i];
if(_17&&_17.format=="date-time"&&typeof it[i]=="string"){
it[i]=dojo.date.stamp.fromISOString(it[i]);
}
}
}
_11=_11["extends"];
}
var _18=it.length;
for(i in it){
if(i==_18){
break;
}
if(it.hasOwnProperty(i)){
val=it[i];
if((typeof val=="object")&&val&&!(val instanceof Date)&&i!="__parent"){
_9=val[_4];
if(!_9||!val.__parent){
val.__parent=it;
}
if(_9){
delete it[i];
var _19=_9.toString().replace(/(#)([^\.\[])/,"$1.$2").match(/(^([^\[]*\/)?[^#\.\[]*)#?([\.\[].*)?/);
if((_9=(_19[1]=="$"||_19[1]=="this"||_19[1]=="")?_1:_7[(_5+_19[1]).replace(_b,"$2$3")])){
if(_19[3]){
_19[3].replace(/(\[([^\]]+)\])|(\.?([^\.\[]+))/g,function(t,a,b,c,d){
_9=_9&&_9[b?b.replace(/[\"\'\\]/,""):d];
});
}
}
if(_9){
val=_9;
}else{
if(!_e){
var _1a;
if(!_1a){
_a.push(_14);
}
_1a=true;
val=_d(val,false,val[_4],true,_17);
val._loadObject=_2.loader;
}
}
}else{
if(!_e){
val=_d(val,_a==it,id===undefined?undefined:_c(id,i),false,_17,_14!=it&&typeof _14[i]=="object"&&_14[i]);
}
}
}
it[i]=val;
if(_14!=it&&!_14.__isDirty){
var old=_14[i];
_14[i]=val;
if(_13&&val!==old&&!_14._loadObject&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")&&i!="$ref"&&!(val instanceof Date&&old instanceof Date&&val.getTime()==old.getTime())&&!(typeof val=="function"&&typeof old=="function"&&val.toString()==old.toString())&&_7.onUpdate){
_7.onUpdate(_14,i,old,val);
}
}
}
}
if(_13&&(_3 in it)){
for(i in _14){
if(!_14.__isDirty&&_14.hasOwnProperty(i)&&!it.hasOwnProperty(i)&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")&&!(_14 instanceof Array&&isNaN(i))){
if(_7.onUpdate&&i!="_loadObject"&&i!="_idAttr"){
_7.onUpdate(_14,i,_14[i],undefined);
}
delete _14[i];
while(_14 instanceof Array&&_14.length&&_14[_14.length-1]===undefined){
_14.length--;
}
}
}
}else{
if(_7.onLoad){
_7.onLoad(_14);
}
}
return _14;
};
if(_1&&typeof _1=="object"){
_1=_d(_1,false,_2.defaultId,true);
_d(_a,false);
}
return _1;
},fromJson:function(str,_1b){
function ref(_1c){
var _1d={};
_1d[this.refAttribute]=_1c;
return _1d;
};
try{
var _1e=eval("("+str+")");
}
catch(e){
throw new SyntaxError("Invalid JSON string: "+e.message+" parsing: "+str);
}
if(_1e){
return this.resolveJson(_1e,_1b);
}
return _1e;
},toJson:function(it,_1f,_20,_21){
var _22=this._useRefs;
var _23=this._addProp;
var _24=this.refAttribute;
_20=_20||"";
var _25={};
var _26={};
function _27(it,_28,_29){
if(typeof it=="object"&&it){
var _2a;
if(it instanceof Date){
return "\""+dojo.date.stamp.toISOString(it,{zulu:true})+"\"";
}
var id=it.__id;
if(id){
if(_28!="#"&&((_22&&!id.match(/#/))||_25[id])){
var ref=id;
if(id.charAt(0)!="#"){
if(it.__clientId==id){
ref="cid:"+id;
}else{
if(id.substring(0,_20.length)==_20){
ref=id.substring(_20.length);
}else{
ref=id;
}
}
}
var _2b={};
_2b[_24]=ref;
return _27(_2b,"#");
}
_28=id;
}else{
it.__id=_28;
_26[_28]=it;
}
_25[_28]=it;
_29=_29||"";
var _2c=_1f?_29+dojo.toJsonIndentStr:"";
var _2d=_1f?"\n":"";
var sep=_1f?" ":"";
if(it instanceof Array){
var res=dojo.map(it,function(obj,i){
var val=_27(obj,_23(_28,i),_2c);
if(typeof val!="string"){
val="undefined";
}
return _2d+_2c+val;
});
return "["+res.join(","+sep)+_2d+_29+"]";
}
var _2e=[];
for(var i in it){
if(it.hasOwnProperty(i)){
var _2f;
if(typeof i=="number"){
_2f="\""+i+"\"";
}else{
if(typeof i=="string"&&(i.charAt(0)!="_"||i.charAt(1)!="_")){
_2f=dojo._escapeString(i);
}else{
continue;
}
}
var val=_27(it[i],_23(_28,i),_2c);
if(typeof val!="string"){
continue;
}
_2e.push(_2d+_2c+_2f+":"+sep+val);
}
}
return "{"+_2e.join(","+sep)+_2d+_29+"}";
}else{
if(typeof it=="function"&&dojox.json.ref.serializeFunctions){
return it.toString();
}
}
return dojo.toJson(it);
};
var _30=_27(it,"#","");
if(!_21){
for(var i in _26){
delete _26[i].__id;
}
}
return _30;
},_addProp:function(id,_31){
return id+(id.match(/#/)?id.length==1?"":".":"#")+_31;
},refAttribute:"$ref",_useRefs:false,serializeFunctions:false};
}
